home *** CD-ROM | disk | FTP | other *** search
- #include <TCL>
- #include "GLOBAL.h"
- #include "CPreferences.h"
- #include "CDiagnostic.h"
- #include "CUtility.h"
- #include "STR.h"
- #include "chars.h" /* Needed for SIGNATURE. */
-
- NEW void CPreferences::IPreferences()
- {
- SysEnvRec theWorld;
- HParamBlockRec params;
- StringPtr folderName, fileName;
- int sysVolNum, dirID, folderVRefNum;
- CInfoPBRec cInfo;
- FInfo fInfo;
- Boolean created;
-
- /*This code mostly lifted from CInstance by Sven Axelsson, which means to
- say that I don't understand much of it... (and hence didn't spot the
- bugs - thanks to Martin Minow for setting me right). */
-
- /*Get system volume information (I guess...). */
- SysEnvirons(1, &theWorld);
- g_Diagnostic("theWorld.sysVRefNum: %d", theWorld.sysVRefNum);
-
- params.volumeParam.ioCompletion = NULL;
- params.volumeParam.ioNamePtr = NULL /*sysVolName*/;
- params.volumeParam.ioVRefNum = theWorld.sysVRefNum;
- params.volumeParam.ioVolIndex = 0;
- gUtility->HardCheckOSError(PBHGetVInfo(¶ms, FALSE));
- sysVolNum = params.volumeParam.ioVRefNum;
- g_Diagnostic("sysVolNum: %d", sysVolNum);
-
- /*Find the preferences folder. */
- folderName = gUtility->BriefGetString(STRcommon, PREFERENCES_index);
- /* The folder's called "Preferences" or whatever's in the STR#...
- N.B. We can't call BriefGetString again while we still need
- folderName. */
-
- cInfo.dirInfo.ioCompletion = NULL;
- cInfo.dirInfo.ioNamePtr = folderName;
- cInfo.dirInfo.ioVRefNum = theWorld.sysVRefNum;
- cInfo.dirInfo.ioFDirIndex = 0;
- cInfo.dirInfo.ioDrDirID = 0;
-
- if (PBGetCatInfo(&cInfo, FALSE) == fnfErr) {
- g_Diagnostic("Prefs folder does not exist");
- /*Folder does not exist; create it. */
- params.fileParam.ioCompletion = NULL;
- params.fileParam.ioNamePtr = folderName;
- params.fileParam.ioVRefNum = theWorld.sysVRefNum;
- params.fileParam.ioFVersNum = 0;
- params.fileParam.ioFDirIndex = 0;
- params.fileParam.ioDirID = 0;
- gUtility->HardCheckOSError(PBDirCreate(¶ms, FALSE));
- dirID = params.fileParam.ioDirID;
- } else {
- g_Diagnostic("Prefs folder exists");
- dirID = cInfo.dirInfo.ioDrDirID;
- }
-
- /*Find the preference file in preference folder, or create it. */
- fileName = gUtility->ApplicationName();
- /* We give the file the same name as the application. */
- if (HGetFInfo(sysVolNum, dirID, fileName, &fInfo) == fnfErr) {
- g_Diagnostic("Prefs file does not exist");
- created = TRUE;
- gUtility->HardCheckOSError(
- HCreate(sysVolNum, dirID, fileName, SIGNATURE, PREFS_TYPE)
- );
- } else {
- g_Diagnostic("Prefs file exists");
- created = FALSE;
- }
-
- gUtility->HardCheckOSError(OpenWD(sysVolNum, dirID, 0, &folderVRefNum));
-
- if (created) {
- HCreateResFile(sysVolNum, dirID, fileName);
- gUtility->HardCheckResError();
- }
-
- itsFileRefNum = HOpenResFile(sysVolNum, dirID, fileName, fsRdWrPerm);
- if (itsFileRefNum == -1) gUtility->HardCheckResError();
- }
-
- PRIVATE Handle CPreferences::Find(ResType type, int ID)
- {
- Handle han = Get1Resource(type, ID);
- OSErr err;
-
- if (han == NULL) { /* Either we can't find it, or we had an error. */
- err = ResError();
- if (err == noErr || err == resNotFound)
- return NULL; /* Couldn't find it (not a real problem). */
- else
- gUtility->HardCheckOSError(err);
- /* A real error of some kind... */
- } else /* Success. */
- return han;
- }
-
- NEW Handle CPreferences::Get(ResType type, int ID)
- {
- Handle han = Find(type, ID);
-
- if (han != NULL) DetachResource(han);
- return han;
- }
-
- NEW void CPreferences::Put(Handle han, ResType type, int ID, StringPtr name)
- {
- Handle old = Find(type, ID);
-
- if (old != NULL) { /* Delete old one. Can this cause fragmentation? */
- RmveResource(old);
- gUtility->HardCheckResError();
- DisposHandle(old);
- }
-
- HandToHand(&han); /* Take a copy, so we don't foul up the handle. */
- AddResource(han, type, ID, name);
- gUtility->HardCheckResError();
- }
-
- OVERRIDE void CPreferences::Dispose()
- {
- CloseResFile(itsFileRefNum);
- gUtility->HardCheckResError();
-
- inherited::Dispose();
- }
-